package com.founder.bdyx.modules.sys.controller;

import com.founder.bdyx.core.entity.ProcessResult;
import com.founder.bdyx.core.result.PageResult;
import com.founder.bdyx.core.util.DictConvertUtil;
import com.founder.bdyx.modules.sys.model.ApplyInfo;
import com.founder.bdyx.modules.sys.service.ApplyInfoService;
import com.founder.bdyx.wxpro.conf.WxConfig;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

import static com.founder.bdyx.core.entity.ProcessResult.ERROR;

@RestController
@RequestMapping("/sys/pickup")
public class PickUpController {
    Log log = LogFactory.getLog(PickUpController.class);

    @Autowired
    private ApplyInfoService applyInfoService;
    @Autowired
    private WxConfig wxConfig;

    @ApiOperation(value = "审批列表视图",notes = "审批列表视图")
    @GetMapping("/list")
    public ModelAndView list() {
        return new ModelAndView("/modules/pickup/list");
    }

    @ApiOperation(value = "获取审核列表",notes = "获取审核列表")
    @RequestMapping(value = "",method = RequestMethod.GET)
    public List<ApplyInfo> getAll(@ApiParam(name = "keyword",value="查询字段",required = false) String keyword, String paystatus) {
        List<ApplyInfo> applyInfos = applyInfoService.getPickUpAll(keyword,paystatus);
        if(applyInfos != null && applyInfos.size()>0)
            DictConvertUtil.converter(applyInfos);
//        return new PageResult(new PageInfo<ApplyInfo>(applyInfos));
        return applyInfos;
    }

    @ApiOperation(value = "申请详情视图",notes = "申请详情视图")
    @RequestMapping(value = "/form",method = RequestMethod.GET)
    public ModelAndView form() {
        return new ModelAndView("/modules/pickup/form");
    }

    @ApiOperation(value = "获取申请详细信息",notes = "获取申请详细信息")
    @PostMapping(value = "/view/{id}")
    public ApplyInfo view(@ApiParam(name = "id", value = "申请单id", required = true) @PathVariable String id){
        ApplyInfo applyInfo = applyInfoService.getByAppId(id);
        DictConvertUtil.converter(applyInfo);
        return applyInfo;
    }

    @ApiOperation(value = "自提通知",notes = "自提通知")
    @PostMapping(value = "/notice/{id}")
    public String notice(@ApiParam(name = "id", value = "申请单id", required = true) @PathVariable Integer id){
        int num = applyInfoService.updatePickUpNoticeById(id);
        if(num > 0) {
            return "ok";
        }
        return "error";
    }

    @ApiOperation(value = "批量自提",notes = "批量自提")
    @PostMapping(value = "/batchNotice")
    public ProcessResult batchNotice(@ApiParam(name = "ids", value = "申请单id数组", required = true) @RequestParam("ids[]") Integer[] ids){
        try {
            applyInfoService.batchPickUpNotice(ids);
            return new ProcessResult();
        }catch (Exception e){
            return new ProcessResult(ERROR,e.getMessage().toString());
        }
    }
}
